package ar.edu.unicen.pladema.vc1.practico2;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

import ar.edu.unicen.pladema.ui.model.ColorRange;
import ar.edu.unicen.pladema.ui.model.points.ScreenPoint;
import ar.edu.unicen.pladema.vc1.fractal.Fractal2;
import ar.edu.unicen.pladema.vc1.model.GraphicAttributes;
import ar.edu.unicen.pladema.vc1.practico.PracticoDrawer;

public class P2Enunciado3Drawer extends PracticoDrawer {
	private Fractal2 fractal;
	private double xMin = Fractal2.REAL_MIN;
	private double xMax = Fractal2.REAL_MAX;
	private double yMin = Fractal2.IMAG_MIN;
	private double yMax = Fractal2.IMAG_MAX;
	private static ColorRange cr=null;
	
	public P2Enunciado3Drawer(GraphicAttributes ga) {
		super(ga);
	}
	public void doZoom(ScreenPoint from, ScreenPoint to) {
		if (from.equals(to)) {
			graphicAttributes.add(GraphicAttributes.LAST_ACTION, "AZOOMRESET");
			xMin = Fractal2.REAL_MIN;
			xMax = Fractal2.REAL_MAX;
			yMin = Fractal2.IMAG_MIN;
			yMax = Fractal2.IMAG_MAX;
			return;
		}
		graphicAttributes.add(GraphicAttributes.LAST_ACTION, "AZOOM");
		xMin = fractal.translateX(from.getX());
		xMax = fractal.translateX(to.getX());
		yMin = fractal.translateY(from.getY());
		yMax = fractal.translateY(to.getY());
	}
	public void doMove(ScreenPoint from, ScreenPoint to) {
		graphicAttributes.add(GraphicAttributes.LAST_ACTION, "AMOVE");
		double xf = fractal.translateX(from.getX());
		double xt = fractal.translateX(to.getX());
		double yf = fractal.translateY(from.getY());
		double yt = fractal.translateY(to.getY());
		
		double xDiff = xf - xt;
		double yDiff = yf - yt;
		xMin += xDiff;
		xMax += xDiff;
		yMin += yDiff;
		yMax += yDiff;
	}
	@Override
	protected void paintControlImpl(PaintEvent e) {
		if (cr == null) {
			cr = testPaleta();
		}
		int mi = graphicAttributes.getInt(GraphicAttributes.MAX_ITERATIONS);
		fractal = new Fractal2(e.width, e.height,
				xMin, xMax,
				yMin, yMax, mi);
		
		for (int i=0; i<e.width; i++ ) {
			for (int j=0; j<e.height; j++) {
				int c = fractal.doCount(i, j);
				
				Color color = null;
				if (c == -1 ) {
					color = new Color (e.gc.getDevice(), 0, 255, 0);
				} else {
					color = new Color (e.gc.getDevice(), cr.get(c));
				}
				e.gc.setForeground(color);
				e.gc.drawPoint(i, j);
			}
		}
	}
	public void setColorRange(ColorRange colorRange) {
		this.cr = colorRange;
	}
	public ColorRange testPaleta() {
		ColorRange testCR = new ColorRange(0, new RGB(0,0,0));
		testCR.add(5, new RGB(0,148,65));
		testCR.add(15, new RGB(175,255,39));
		testCR.add(30, new RGB(255,255,0));
		testCR.add(45, new RGB(255,190,0));
		testCR.add(60, new RGB(255,0,120));
		testCR.add(75, new RGB(240,30,200));
		testCR.add(95, new RGB(160,30,240));
		testCR.add(130, new RGB(80,15,240));
		testCR.add(200, new RGB(0,0,255));
		testCR.add(230, new RGB(140,225,225));
		testCR.add(254, new RGB(0,255,255));
		testCR.add(255, new RGB(255,255,255));
		testCR.add(500, new RGB(0,0,0));
		return testCR;
	}
}
